package com.atguigu.network;

import java.io.*;
import java.net.Socket;

public class Demo04TCPClientBetter {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("127.0.0.1", 8888);

        // 1、向服务器端发送数据
        OutputStream outputStream = socket.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);

        printStream.print("客户端给服务器端发送的数据~~~");

        socket.shutdownOutput();

        // 2、接收服务器端返回的响应
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        StringBuffer stringBuffer = new StringBuffer();
        String line = null;
        while((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line);
        }

        System.out.println("客户端接收到服务器端的响应数据：" + stringBuffer);

        socket.close();
    }

}
